﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.Common;

namespace NUS.ISS.ARS.BusinessComponent
{
    public  class AircraftBC:BaseBC
    {
        public ICollection<Aircraft> GetAllAircraft()
        {
            try
            {
                using (var ent = new Entities())
                {
                    return ent.Aircraft.ToList();
                }
            }
            catch (Exception ex)
            {
                //Todo: Log Exception
            }

            return null;
        }

        public IEnumerable<Aircraft> IEnumGetAllAircraft()
        {
            using (var ent = new Entities())
            {
                return ent.Aircraft.ToList();
            }
        }
        public Aircraft GetAirPlaneById(int id)
        {
            try
            {
                using (var ent = new Entities())
                {
                    var aircraft = ent.Aircraft.FirstOrDefault(a => a.AircraftId == id);

                    if (aircraft != null)
                    {
                        return aircraft;
                    }
                }
            }
            catch (Exception ex)
            {
                //Todo: Log Exception
            }

            return null;
        }

        public SaveResult SaveAircraft(Aircraft aircraft)
        {
            var saveStatus = Constants.SaveStatus.Success;
            try
            {
                if (aircraft.IsValid)
                {
                    using (var ent = new Entities())
                    {
                        ent.Aircraft.Add(aircraft);
                        ent.SaveChanges();
                    }
                }
                else
                {
                    SetError("Aircraft is invalid.");
                    return new SaveResult()
                    {
                        Status = Constants.SaveStatus.Failed,
                        Message = ErrorMessage
                    }; 
                }
            }
            catch (Exception)
            {
                //Todo: Log Exception
                return new SaveResult()
                {
                    Status = Constants.SaveStatus.Failed,
                     Message="Database Error..."
                };
            }

            return  new SaveResult()
            {
                Status = Constants.SaveStatus.Success,
                    Message=""
            };
        }

    }
}
